#pragma once
#include "RetrieverMaster.h"
#include "../../PatchCluster/Data/CodeBook.h"
#include "../../PatchCluster/FeatureExtract/FeatureExtract.h"

// CRetriverSlave

enum PatchMode
{
	PatchModeNonOverlapped,
	PatchModeHalfOverlapped,
	PatchModePerPixel
};

class CRetriverSlave : public CWinThread
{
	DECLARE_DYNCREATE(CRetriverSlave)

	CRetrieverMaster*		m_Master;
	BOOL					m_bContinue;
	
	// parameters
	CCodeBook*				m_clusterCodeBook;
	CFeatureExtract*		m_extractor;
	INT						m_iPatchSize;		// width and height of a Patch
	PatchMode				m_patchMode;
	INT						m_iRegionThreshold;	// how many patch will create an interested region?

	// GDI+ related
	Font*					m_fntFrameIdx;
	Brush*					m_brFrameIdx;
	CLSID					m_pngClsid;
	CString					m_sTempFile;
	char					m_sAnsiTempFile[MAX_PATH];
	char*					m_sSIFTKmeanModel;
	char*					m_sSIFTSVMModel;

	void ProcessFrame(BitmapFrame* frame);
	static BOOL FindCandicate(CArray<CArray<CPoint>*>& arr, int x, int y, CArray<int>& grps);
	Rect GetBoundingBox(CArray<CPoint>* candicates);
	BOOL CheckLowFeature(Bitmap& bmp);
	static int GetEncoderClsid(const WCHAR* format, CLSID* pClsid);
	static void CString2Char(CString& s, char arr[]);
	
protected:
	CRetriverSlave();           // protected constructor used by dynamic creation
	virtual ~CRetriverSlave();

	DECLARE_MESSAGE_MAP()

public:

	void SetMaster(CRetrieverMaster* master);
	void SetParams(CCodeBook* codeBook, CFeatureExtract* extractor, int iPatchSize = 16,
		PatchMode patchMode = PatchModeNonOverlapped, int regionThreshold = 4,
		char* siftKmeansModel = NULL, char* siftSVMModel = NULL);

	virtual BOOL InitInstance();
	virtual int ExitInstance();

	void OnWmSlaveMessage(WPARAM wParam, LPARAM lParam);
	virtual int Run();
};


